package indi.caibo.service_ucenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.R;
import indi.caibo.commonutils.vo.UcenterMemberPay;
import indi.caibo.service_ucenter.entity.UcenterMember;
import indi.caibo.service_ucenter.entity.vo.backend.LoginVo;
import indi.caibo.service_ucenter.entity.vo.backend.RegisterVo;
import indi.caibo.service_ucenter.service.UcenterMemberService;
import indi.caibo.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Bo Cai
 * @since 2021-12-01
 */
@Api(tags = "会员管理")
@RestController
@RequestMapping("/ucenter-service/member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("get-login-info")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember loginInfoVo = ucenterMemberService.getLoginInfo(memberId);
            return R.ok().data("loginInfo", loginInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "登录失败：获取token失败");
        }
    }

    @ApiOperation(value = "根据token获取收藏的课程")
    @GetMapping("get-collected-course")
    public R getCollectedCourse(HttpServletRequest request) {
        try {
            String memberId = JwtUtils.getMemberIdByJwtToken(request);
            UcenterMember loginInfoVo = ucenterMemberService.getLoginInfo(memberId);
            return R.ok().data("loginInfo", loginInfoVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "登录失败：获取token失败");
        }
    }

    @ApiOperation(value = "根据用户id获取用户")
    @GetMapping("{id}")
    public UcenterMemberPay getInfo(@PathVariable String id) {
        //根据用户id获取用户信息
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMemberPay memeber = new UcenterMemberPay();
        BeanUtils.copyProperties(ucenterMember, memeber);
        return memeber;
    }

    @ApiOperation(value = "统计某一天的注册人数")
    @GetMapping(value = "count-register/{day}")
    public R registerCount(
            @PathVariable String day) {
        Integer count = ucenterMemberService.countRegisterByDay(day);
        return R.ok().data("countRegister", count);
    }

    @ApiOperation(value = "更新用户信息")
    @PostMapping(value = "update")
    public R update(@RequestBody UcenterMember ucenterMember) {
        ucenterMemberService.updateById(ucenterMember);
        return R.ok().message("用户信息更新成功！");
    }
}

